﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Serialization;
using DependencyInjection;

namespace Settings
{
    public class SettingsManager
    {
        protected Dictionary<string, BaseSettings> _mapSettings = new Dictionary<string,BaseSettings>();

        public BaseSettings this[string key]
        {
            get
            {
                BaseSettings result = null;

                if (_mapSettings.ContainsKey(key))
                {
                    result = _mapSettings[key];
                }

                return result;
            }

            set
            {
                if (!_mapSettings.ContainsKey(key))
                {
                    _mapSettings.Add(key, value);
                }
                else
                {
                    _mapSettings[key] = value;
                }
            }
        }

        protected ISerializer _serializer;

        [Inject]
        public SettingsManager(ISerializer serializer)
        {
            _serializer = serializer;
        }

        public void Save(string filename)
        {
            BaseSettings settings = this[filename];
            if (settings != null)
            {
                _serializer.Save<BaseSettings>(filename, settings);
            }
        }
        
        public void Load(string filename)
        {
            if (!_mapSettings.ContainsKey(filename))
            {
                BaseSettings settings = _serializer.Load<BaseSettings>(filename);

                _mapSettings.Add(filename, settings);
            }
        }
    }
}
